package com.edu.app.service.impl.wechat.msgroute.impl.MessageHandler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.app.bean.core.SysTenant;
import com.edu.app.bean.core.SysUser;
import com.edu.app.bean.core.SysWxUser;
import com.edu.app.bean.linkbeans.SysUserWxUserBind;
import com.edu.app.mapper.core.SysUserMapper;
import com.edu.app.mapper.core.SysWxUserMapper;
import com.edu.app.mapper.linkmappers.SysUserWxUserBindMapper;
import com.edu.app.model.wechat.WxErrorException;
import com.edu.app.service.impl.wechat.msgroute.WxMpMessageHandler;
import com.edu.app.service.impl.wechat.msgroute.consts.WxConsts;
import com.edu.app.service.impl.wechat.msgroute.session.WxSessionManager;
import com.edu.app.service.sys.core.SysTenantService;
import com.edu.app.service.sys.wechat.WechatBaseService;
import com.edu.app.service.sys.wechat.WechatUserService;
import com.edu.app.threadlocal.TenantContext;
import com.edu.app.util.json.JsonUtil;

import org.springframework.beans.BeanWrapperImpl;
import org.springframework.context.ApplicationContext;

import java.io.IOException;
import java.util.Map;


/**
 * Created by qianjia on 15/1/22.
 */
public class QrSubscribeHandler implements WxMpMessageHandler {
  @Override
  public Map<String,String> handle(Map<String,String> wxMessage,
                                   Map<String, Object> context,
                                   WechatBaseService wxMpService,
                                   WxSessionManager sessionManager) {
      System.out.println("subscribe event");
      ApplicationContext applicationContext = (ApplicationContext)context.get("appContext");

      String openId        = wxMessage.get(WxConsts.MsgField.FromUserName);
      String scenceStr  = wxMessage.get(WxConsts.MsgField.EventKey).substring(WxConsts.SceneType.QRPREFIXLEN);
      String[] userInfo = scenceStr.split("_");

      //Query user info
      WechatUserService wechatUserService  = applicationContext.getBean(WechatUserService.class);
      SysWxUserMapper sysWxUserMapper                  = applicationContext.getBean(SysWxUserMapper.class);
      SysUserMapper sysUserMapper                    = applicationContext.getBean(SysUserMapper.class);
      SysUserWxUserBindMapper sysUserWxUserBindMapper          = applicationContext.getBean(SysUserWxUserBindMapper.class);
      BeanWrapperImpl beanWrapper          = applicationContext.getBean(BeanWrapperImpl.class);
      SysTenantService sysTenantService    = applicationContext.getBean(SysTenantService.class);
      try {
          QueryWrapper<SysUser> queryMapper = new QueryWrapper<SysUser>();
          String userName   = userInfo[1];
          Integer tenantId  = Integer.valueOf(userInfo[0]);

          SysTenant sysTenant = sysTenantService.getObjectById(tenantId);
          TenantContext.setCurrentTenant(sysTenant);
          queryMapper.lambda().eq(SysUser::getUsername,userName);

          SysUser sysUser    = sysUserMapper.selectOne(queryMapper);
          String  userinfo      = wechatUserService.userInfo(openId);
          Map<String,Object> userInfoMap    = JsonUtil.fromJson(userinfo,Map.class);
          SysWxUser wxUser       = new SysWxUser();

          wxUser.initializeSystemColumns(userName);
          beanWrapper.setBeanInstance(wxUser);
          beanWrapper.setPropertyValues(userInfoMap);

          sysWxUserMapper.insert(wxUser);

          SysUserWxUserBind sysUserWxUserBind = new SysUserWxUserBind();
          sysUserWxUserBind.initializeSystemColumns(userName);

          sysUserWxUserBind.setUser_id(sysUser.getId());
          sysUserWxUserBind.setWxuser_id(wxUser.getId());

          sysUserWxUserBindMapper.insert(sysUserWxUserBind);

      }catch(WxErrorException wxe){

      }catch(IOException ioe){

      }finally {
          TenantContext.setCurrentTenant(null);
      }
      System.out.println(wxMessage.get(WxConsts.MsgField.EventKey));
      System.out.println(wxMessage.get(WxConsts.MsgField.Event));
      System.out.println(wxMessage.get(WxConsts.MsgField.FromUserName));

      return null;

  }

}
